package com.guancloud.gradesheet.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ExcelUtil {
	public static List<List<String>> readExcel(File file, int sheetNum, int col) throws Exception {
		List<List<String>> result = new ArrayList();
		// create a file input stream object
		FileInputStream is = new FileInputStream(file);
		// create a POI file-system object
		POIFSFileSystem ts = new POIFSFileSystem(is);
		// get document object
		HSSFWorkbook wb = new HSSFWorkbook(ts);
		// get sheet object
		HSSFSheet sheet = wb.getSheetAt(sheetNum);
		// row object
		HSSFRow row = null;
		for(int i=0; sheet.getRow(i)!=null; i++) {
			row = sheet.getRow(i);
			List<String> cellList = new ArrayList(); 
			// get every cell
			for (int j=0; row.getCell(j)!=null && j<col; j++) {
				String str = row.getCell(j).toString();
				System.out.println(str);
				cellList.add(str);
			}
			result.add(cellList);
		}
		is.close();
		
		return result;
	}
}
